// 商家端全局状态（从 MerchantApp 迁移为 Pinia，空实现占位，不包含模拟数据）
import { defineStore } from 'pinia'
import { STORAGE_KEYS } from '@/constants'

export const useAppStore = defineStore('app', {
  state: () => ({
    userInfo: null,
    isLoggedIn: false,
    accessToken: '',
    shopProfile: null,
    notifications: 0,
  }),
  getters: {
    isAuthenticated: (s) => s.isLoggedIn && !!s.accessToken,
    userName: (s) => s.userInfo?.nickname || s.userInfo?.username || '未登录',
  },
  actions: {
    initializeState(){
      try{
        const u = JSON.parse(localStorage.getItem(STORAGE_KEYS.USER_INFO) || 'null')
        if (u){ this.userInfo=u; this.isLoggedIn=true }
        const t = localStorage.getItem(STORAGE_KEYS.ACCESS_TOKEN) || ''
        if (t) this.accessToken=t
      }catch(e){ console.warn('初始化状态失败', e) }
    },
    setUserInfo(u){ this.userInfo=u; this.isLoggedIn=!!u; u?localStorage.setItem(STORAGE_KEYS.USER_INFO, JSON.stringify(u)):localStorage.removeItem(STORAGE_KEYS.USER_INFO) },
    setAccessToken(t){ this.accessToken=t; t?localStorage.setItem(STORAGE_KEYS.ACCESS_TOKEN,t):localStorage.removeItem(STORAGE_KEYS.ACCESS_TOKEN) },
    logout(){ this.setUserInfo(null); this.setAccessToken(''); this.router?.push('/dashboard') },
  }
})

